package com.ruoyi.ap.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.ruoyi.ap.pojo.vo.ProductVo;
import com.ruoyi.ap.service.IProductCategoryService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ap.domain.Product;
import com.ruoyi.ap.service.IProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品Controller
 * 
 * @author HuaQP
 * @date 2023-09-11
 */
@RestController
@RequestMapping("/ap/product")
public class ProductController extends BaseController
{
    @Resource
    private IProductService productService;

    @Resource
    private IProductCategoryService categoryService;

    /**
     * 查询产品列表
     */
    @PreAuthorize("@ss.hasPermi('ap:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductVo product)
    {
        startPage();
        List<ProductVo> list = productService.selectProductList(product);
        return getDataTable(list);
    }

    /**
     * 导出产品列表
     */
    @PreAuthorize("@ss.hasPermi('ap:product:export')")
    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductVo product)
    {
        List<ProductVo> list = productService.selectProductList(product);
        ExcelUtil<ProductVo> util = new ExcelUtil<>(ProductVo.class);
        util.exportExcel(response, list, "产品数据");
    }

    /**
     * 获取产品详细信息
     */
    @PreAuthorize("@ss.hasPermi('ap:product:query')")
    @GetMapping(value = {"/", "/{id}"})
    public AjaxResult getInfo(@PathVariable(value = "id",required = false) Long id)
    {
        AjaxResult ajax = AjaxResult.success();

        ajax.put("categorys",categoryService.selectCategoryAll());
        if (StringUtils.isNotNull(id))
        {
            ajax.put(AjaxResult.DATA_TAG, productService.selectProductById(id));
        }
        return ajax;
    }

    /**
     * 新增产品
     */
    @PreAuthorize("@ss.hasPermi('ap:product:add')")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid Product product)
    {
        return toAjax(productService.insertProduct(product));
    }

    /**
     * 修改产品
     */
    @PreAuthorize("@ss.hasPermi('ap:product:edit')")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Valid Product product)
    {
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 删除产品
     */
    @PreAuthorize("@ss.hasPermi('ap:product:remove')")
    @Log(title = "产品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productService.deleteProductByIds(ids));
    }

    // 获取产品树列表
    @PreAuthorize("@ss.hasPermi('ap:product:list')")
    @GetMapping("/productTree")
    public AjaxResult productTree()
    {
        return success(productService.selectProductTreeList());
    }

}
